package org.zhixiang.gisQuery;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.zhixiang.gisQuery.query.RectangleMBR;
import org.zhixiang.gisQuery.query.SelfPoint;
import org.zhixiang.gisQuery.rtree.Rectangle;
import org.zhixiang.gisQuery.utils.JacksonUtil;

public class GisConfig {
	
	public static void main(String[] args) throws IOException {
	initPoiDatas("F:\\workspace\\gisQuery\\src\\main\\resources\\gisDatas");
		
		
	}
	
	private static void initPoiDatas(String gisDataDirStr) throws IOException {
		File gisDataDir = new File(gisDataDirStr);
		if (!gisDataDir.exists() || !gisDataDir.isDirectory()) {
			System.out.println("init failure , " + gisDataDirStr + " not exists or not a dir");
			return;
		}
		File[] listFiles = gisDataDir.listFiles(new FilenameFilter() {
			@Override
			public boolean accept(File dir, String name) {
				return name.endsWith("json");
			}
		});

		for (File file : listFiles) {
			String readFileToString = FileUtils.readFileToString(file);
			List<RectangleMBR> parseDis = JacksonUtil.str2List(readFileToString, RectangleMBR.class); // GaoDeJsonDataParser.parseJsonDis2();
			for (RectangleMBR mbr : parseDis) {
				configMbr(mbr);
			}
			FileUtils.write(file, JacksonUtil.obj2Str(parseDis));
		}
	}
	
	private static void configMbr(RectangleMBR mbr) {
		List<SelfPoint> polyline = mbr.getPolyline();
		double minX = Double.MAX_VALUE;
		double maxX = Double.MIN_VALUE;
		double minY = Double.MAX_VALUE;
		double maxY = Double.MIN_VALUE;
		for (SelfPoint selfPoint : polyline) {
			if (selfPoint.x < minX) {
				minX = selfPoint.x;
			}
			if (selfPoint.x > maxX) {
				maxX = selfPoint.x;
			}
			if (selfPoint.y < minY) {
				minY = selfPoint.y;
			}
			if (selfPoint.y > maxY) {
				maxY = selfPoint.y;
			}
		}
		Rectangle re = new Rectangle(minX, minY, maxX, maxY);
		mbr.setRectangle(re);
		mbr.initMinBound(150, 150);
	}

}
